package com.lzxuni.modules.basics.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzxuni.modules.basics.entity.Idgenerator;
import com.lzxuni.modules.basics.mapper.IdgeneratorMapper;
import com.lzxuni.modules.basics.service.IdgeneratorService;
import org.springframework.stereotype.Service;

/**
 * 根据name获取递增的6位ID值
 * @author liuzp
 * @since 2019-10-29
 */
@Service
public class IdgeneratorServiceImpl extends ServiceImpl<IdgeneratorMapper, Idgenerator> implements IdgeneratorService {



    @Override
    public Integer getFourSequence(String name) {
        Idgenerator idgenerator = this.getOne(new QueryWrapper<Idgenerator>().eq("name",name));
        Idgenerator idgenerator1 = new Idgenerator();
        if(idgenerator == null ){
            idgenerator1.setName(name);
            idgenerator1.setValue(1002);
            idgenerator1.setRev(1);
            this.save(idgenerator1);
            return 1001;
        }else{
            Integer value = idgenerator.getValue();
            if (value == 9999) {
                idgenerator1.setValue(1001);
            }else{
                idgenerator1.setValue(value + 1);
            }
            idgenerator1.setRev(idgenerator.getRev() + 1);
            this.update(idgenerator1, new QueryWrapper<Idgenerator>().eq("name", name));
            return value;
        }
    }

    @Override
    public Integer getThreeSequence(String name) {
        Idgenerator idgenerator = this.getOne(new QueryWrapper<Idgenerator>().eq("name",name));
        Idgenerator idgenerator1 = new Idgenerator();
        if(idgenerator == null ){
            idgenerator1.setName(name);
            idgenerator1.setValue(102);
            idgenerator1.setRev(1);
            this.save(idgenerator1);
            return 101;
        }else{
            Integer value = idgenerator.getValue();
            if (value == 999) {
                idgenerator1.setValue(101);
            }else{
                idgenerator1.setValue(value + 1);
            }
            idgenerator1.setRev(idgenerator.getRev() + 1);
            this.update(idgenerator1, new QueryWrapper<Idgenerator>().eq("name", name));
            return value;
        }
    }
}
